package com.hzmall.controller;

import com.hzmall.common.Const;
import com.hzmall.common.ServerResponse;
import com.hzmall.pojo.User;
import com.hzmall.service.impl.UserService;
import com.hzmall.util.CheckLoginUtil;
import com.hzmall.util.CookieUtil;
import com.hzmall.util.JsonUtil;
import com.hzmall.util.RedisShardedPoolUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/user/")
public class UserController {
    @Autowired
    private UserService userService;

    //用户登录
    @RequestMapping(value = "login.do", method = RequestMethod.POST)
        @ResponseBody
        public ServerResponse login(String username, String password, HttpSession session, HttpServletResponse httpServletResponse){
            ServerResponse response = this.userService.login(username, password);
            User user = (User) response.getData();
            if (user != null && response.isSuccess()){
                CookieUtil.setCookie(httpServletResponse,session.getId());
                RedisShardedPoolUtil.setEx(session.getId(), JsonUtil.obj2String(user),Const.CookieExpireTime);
                return response;
            }
            return response;
    }

    //用户注册
    @RequestMapping(value = "register.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse register(User user){
        return this.userService.register(user);
    }

    //用户注销
    @RequestMapping(value = "logout.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse logout(HttpSession session, HttpServletRequest request, HttpServletResponse response) {
        User currentUser = CheckLoginUtil.doCheckLogin(request);
        if (currentUser != null) {
            return this.userService.logout(request, response);
        }
        return ServerResponse.createByErrorMessage("当前没有找到用户登录信息");
    }

    //获取用户信息
    @RequestMapping(value = "get_user_info.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse getUserInfo(HttpServletRequest request){
        String token = CookieUtil.readCookie(request);
        String userStr = RedisShardedPoolUtil.get(token);
        User user = JsonUtil.string2Obj(userStr, User.class);
        if (user != null){
            user.setPassword(StringUtils.EMPTY);
            return ServerResponse.createBySuccess(user);
        }
        return ServerResponse.createByErrorMessage("用户未登录,无法获取当前用户信息");
    }

    //获取用户重置密码问题
    @RequestMapping(value = "forget_get_question.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse forgetGetQuestion(String username){
        return this.userService.forgetGetQuestion(username);
    }

    //检查提交问题答案
    @RequestMapping(value = "forget_check_answer.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse forgetCheckAnswer(String username, String question, String answer){
        return this.userService.forgetCheckAnswer(username, question, answer);
    }

    //忘记密码的重设密码
    @RequestMapping(value = "forget_reset_password.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse forgetResetPassword(String username, String passwordNew, String forgetToken){
        return this.userService.forgetResetPassword(username, passwordNew, forgetToken);
    }

    //登录状态下重置密码
    @RequestMapping(value = "reset_password.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse resetPassword(HttpServletRequest request, String passwordOld, String passwordNew){
        String token = CookieUtil.readCookie(request);
        String userStr = RedisShardedPoolUtil.get(token);
        User user = JsonUtil.string2Obj(userStr, User.class);
        if (user != null){
            return this.userService.resetPassword(passwordOld,passwordNew,user);
        }
        return ServerResponse.createByErrorMessage("用户未登录!!");
    }

    //登录状态修改个人信息
    @RequestMapping(value = "update_information.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse updateInformation(User user, HttpServletRequest request){
        User currentUser = CheckLoginUtil.doCheckLogin(request);
        if (currentUser == null){
            return ServerResponse.createByErrorMessage("用户未登录");
        }
        user.setId(currentUser.getId());
        return this.userService.updateInformation(user);
    }

    //获取登录用户详细信息，若未登录则返回code=10，强制前端跳转登录页面
    @RequestMapping(value = "get_information.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse getInformation(HttpServletRequest request){
        User currentUser = CheckLoginUtil.doCheckLogin(request);
        if (currentUser == null){
            return ServerResponse.createByErrorCodeMessage(Const.STATUS_CODE.NEED_LOGIN.getCode(),Const.STATUS_CODE.NEED_LOGIN.getDesc());
        }
        return ServerResponse.createBySuccess(currentUser);
    }

    //检查用户名或者邮箱
    @RequestMapping(value = "check_valid.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse checkValid(String str, String type){
        return this.userService.check_valid(str, type);
    }
}
